var config = require('../../config.js');
var request = require('../../utils/request.js');
var expressutil = require('../../utils/express.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    expressDetail: [],//物流信息
    expressStr: '',//物流字符串
    expressCompany: {},
    goodsImgUrl:'',//商品图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.expressDetailAction();
    this.orderDetail();
  },

  /**
   * 重置时间
   */
  resetExpressDate: function () {
    var detail = this.data.expressDetail;
    detail.map(item => {
        var datetime = item.time;
        item.date = this.stringToDate(datetime);
        item.datetime = this.stringToTime(datetime);
    });
    this.setData({
      expressDetail : detail
    })
  },


  stringToDate:function(datestr){
    var strs = datestr.split(' ');
    return strs[0];
  },

  stringToTime:function(datestr){
    var strs = datestr.split(' ');
    return strs[1];
  },


  /**
   * 物流详情
   */
  expressDetailAction: function () {
    var self = this;
    expressutil.expressDetailAction(this.data.orderId)
      .then(data => {
        self.setData({
          expressStr: JSON.stringify(data),
          expressDetail: JSON.parse(data.detail),
          expressCompany: data.company
        });
        console.log(self.data);
        self.resetExpressDate();
      }).catch(msg => {
        wx.showToast({
          title: msg || '',
          icon: 'none'
        })
      });
  },


  callPhone:function(event){
    console.log(event);
    wx.makePhoneCall({
      phoneNumber: event.currentTarget.dataset.phone,
    });
  },


  orderDetail:function(){
    var url = config.orderDetailUrl;
    let params = {
      orderId: this.data.orderId
    };
    var self = this;
    request.requestPost(url, params)
      .then(data => {
        console.log(data);
        self.setData({
          goodsImgUrl: data.goods[0].IMG,
        });
      }).catch(msg => {
        console.log(msg);
      });
  },

})